package com.houtai.controller.article.cate;


import com.aliyun.openservices.oss.model.ObjectMetadata;
import com.houtai.authority.Authority;
import com.houtai.authority.AuthorityType;
import com.houtai.base.RetInfo;
import com.houtai.bean.TsArticleCate;
import com.houtai.bean.TsJzAdmin;
import com.houtai.bean.dto.article.comments.TsArticleCommentsListDto;
import com.houtai.common.Contant;
import com.houtai.common.oss.OssUtil;
import com.houtai.service.article.cate.ArticleCateService;
import com.houtai.service.funcion.FuncionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;


@Controller
@RequestMapping(value = {"v5"})
public class ArticleCateController {

    @Resource
    private FuncionService funcionService;
    @Resource
    private ArticleCateService articleCateService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @Purpose  查找文章类别
     * @param
     * @version  3.4.0
     * @author   lizhun
     * @time     2018-07-10
     * @return   ModelAndView
     */
    @Authority(AuthorityType.Login)
    @RequestMapping(value = "/article/cate/list")
    public ModelAndView list(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView("article/cate/list");
        mv.addObject("token", request.getParameter("token"));
        TsJzAdmin tsJzAdmin = (TsJzAdmin) redisTemplate.opsForValue().get(request.getParameter("token")) ;
        mv.addObject("funcions", funcionService.findSonFuncion(tsJzAdmin, Integer.valueOf(request.getParameter("father_id"))));
        mv.addObject("articleCates", articleCateService.findArticleCates());
        return mv;
    }
    /**
     * @Purpose  跳转到添加文章类别页面
     * @param
     * @version  3.4.0
     * @author   lizhun
     * @time     2018-07-10
     * @return   ModelAndView
     */
    @Authority(AuthorityType.Login)
    @RequestMapping(value = "/article/cate/insert/jump")
    public ModelAndView insertj(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView("article/cate/insert");
        mv.addObject("token", request.getParameter("token"));
        return mv;
    }
    /**
     * @Purpose  跳转到更新文章类别页面
     * @param
     * @version  3.4.0
     * @author   lizhun
     * @time     2018-07-10
     * @return   ModelAndView
     */
    @Authority(AuthorityType.Login)
    @RequestMapping(value = "/article/cate/update/jump")
    public ModelAndView updatej(HttpServletRequest request, @RequestParam(value = "cate_id" , required = true)int cate_id ) {
        ModelAndView mv = new ModelAndView("article/cate/update");
        mv.addObject("token", request.getParameter("token"));
        TsArticleCate tsArticleCate = articleCateService.findArticleCateById(cate_id);
        mv.addObject("articleCate", tsArticleCate);

        return mv;
    }
    /**
     * @Purpose  添加文章类别
     * @param    tsArticleCate
     * @version  3.4.0
     * @author   lizhun
     * @time     2018-07-10
     * @return   RetInfo
     */
    @RequestMapping(value="/article/cate/insert", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    @Authority(AuthorityType.Login)
    public RetInfo insert(@Valid TsArticleCate tsArticleCate, BindingResult result, HttpServletRequest request){
        RetInfo retInfo = new RetInfo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = articleCateService.insertArticleCate(tsArticleCate , request);
        }
        return retInfo;

    }
    /**
     * @Purpose  修改文章类别
     * @param    tsArticleCate
     * @version  3.4.0
     * @author   lizhun
     * @time     2018-07-10
     * @return   RetInfo
     */
    @RequestMapping(value="/article/cate/update", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    @Authority(AuthorityType.Login)
    public RetInfo update(@Valid TsArticleCate tsArticleCate, BindingResult result, HttpServletRequest request){
        RetInfo retInfo = new RetInfo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = articleCateService.updateArticleCate(tsArticleCate , request);
        }
        return retInfo;

    }
    /**
     * @Purpose  修改文章类别
     * @param    cate_id
     * @version  3.4.0
     * @author   lizhun
     * @time     2018-07-10
     * @return   RetInfo
     */
    @RequestMapping(value="/article/cate/update/state", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    @Authority(AuthorityType.Login)
    public RetInfo state(Integer cate_id, HttpServletRequest request){
        return articleCateService.updateArticleCateState(cate_id, request);

    }
    /**
     *上传banner图片
     * @param frontFile
     * @return
     */
    @RequestMapping(value = "/article/cate/img" )
    @ResponseBody
    public Map<String, String> img(@RequestParam(value = "frontFile", required = true) MultipartFile frontFile)throws Exception{
        Map<String, String>  map = new HashMap<>();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddhhmmssS", Locale.CHINA);

        String frontFileName = frontFile.getOriginalFilename().substring(frontFile.getOriginalFilename().lastIndexOf(".") + 1);
        String imgName = formatter.format(new Date(System.currentTimeMillis()));
        String frontSavePath = Contant.FILE_IMG+"/" + "article/cate/" + imgName + "." + frontFileName;
        ObjectMetadata frontMetadata = new ObjectMetadata();

        InputStream inputStream = frontFile.getInputStream();
        frontMetadata.setContentLength(inputStream.available());
        OssUtil.uploadInputStream(inputStream, frontMetadata, frontSavePath);
        map.put("mark", "0");
        map.put("tip", Contant.DOMAIN +  Contant.FILE_IMG+"/"+"article"+"/cate/"+imgName + "." + frontFileName);
        map.put("obj", imgName);

        return map;
    }
    /**
     * @Purpose  查找文章评论列表
     * @param
     * @version  3.4.0
     * @author   lizhun
     * @time     2018-07-10
     * @return   ModelAndView
     */
    @Authority(AuthorityType.Login)
    @RequestMapping(value = "/article/comments/list")
    public ModelAndView comments(HttpServletRequest request,TsArticleCommentsListDto dto) {
        ModelAndView mv = new ModelAndView("article/comments/list");
        mv.addObject("token", request.getParameter("token"));
        mv.addObject("retInfo", articleCateService.commentsList(request, dto));
        return mv;
    }
}
